package com.huaxia.day11;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class SetTest {
    public static void main(String[] args) {
//        create();
//        treeSet();
        test();
    }

    public static void create() {
        Set<String> set = new HashSet<String>();
        String obj1 = "hello";
        String obj2 = obj1;
        String obj3 = "word";
        set.add(obj1);
        set.add(obj2);
        set.add(obj3);


        set.remove(obj1);

        System.out.println(set.size());
    }


    public static void test() {
        String obj1 = "hello";
        String obj2 = "he" + "llo";
        String obj3 = new String("hello");
        System.out.println(obj1 == obj2);
        System.out.println(obj1 == obj3);
        System.out.println(obj1.equals(obj2));
        System.out.println(obj1.equals(obj3));

    }

    /**
     * treeSet
     * 匿名比较器
     * 默认升序
     */
    public static void treeSet() {
        Set<Student> set = new TreeSet<>(new Comparator<Student>() {
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        for (int i = 0; i < 10; i++) {
            set.add(new Student("name_" + i, (int) (Math.random() * 100)));
        }

        for (Student st : set) {
            System.out.println(st.toString());
        }
    }
}
